package com.longcai.controller.customer;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.customer.ClyjUserCard;
import com.longcai.mapper.customer.ClyjUserCardMapper;
import com.longcai.service.customer.ClyjUserCardService;
import com.longcai.util.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * <p>
 * 用户银行卡表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-04-18
 */
@Controller
@RequestMapping("/clyjUserCard")
public class ClyjUserCardController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjUserCardService clyjUserCardService;
    @Autowired
    private ClyjUserCardMapper clyjUserCardMapper;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 用户绑定银行卡
     *
     * @param params
     * @return
     */
    @RequestMapping("/saveCard")
    @ResponseBody
    public ApiResult saveCard(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        //银行卡号
        String cardNo = params.getString("cardNo");
        //查询改银行卡是否已绑定
        QueryWrapper<ClyjUserCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", clyjUser.getUserId());
        queryWrapper.eq("cardNo", cardNo);
        ClyjUserCard userCard = this.clyjUserCardMapper.selectOne(queryWrapper);
        if (userCard != null) {
            logger.error("该银行卡已绑定");
            return ApiResult.fail("该银行卡已绑定");
        }
        return this.clyjUserCardService.saveCard(clyjUser.getUserId(), params, cardNo);
    }

    /**
     * 获取银行卡列表
     *
     * @return
     */
    @RequestMapping("/getCardList")
    @ResponseBody
    public ApiResult getCardList(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        return this.clyjUserCardService.getCardList(clyjUser.getUserId());
    }

    /**
     * 根据id获取银行卡信息
     *
     * @param params
     * @return
     */
    @RequestMapping("/getCardInfo")
    @ResponseBody
    public ApiResult getCardInfo(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        String id = params.getString("id");
        if (id == null || id.equals("")) {
            return ApiResult.fail("选择错误");
        }
        //查询用户其他银行卡 有默认改为为选中状态
        QueryWrapper<ClyjUserCard>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",clyjUser.getUserId());
        List<ClyjUserCard>clyjUserCardList=clyjUserCardMapper.selectList(queryWrapper);
        for (ClyjUserCard clyjUserCard:clyjUserCardList) {
                  if(clyjUserCard.getFlag()==0){
                      clyjUserCard.setFlag(1);
                      clyjUserCardMapper.updateById(clyjUserCard);
                  }
        }
        //选择的银行卡设置为默认
        ClyjUserCard clyjUserCard = this.clyjUserCardService.selectById(id);
        clyjUserCard.setFlag(0);
        clyjUserCardMapper.updateById(clyjUserCard);
        return ApiResult.success(clyjUserCard);
    }
}